Images play a vital role in web development, improving the visual appeal and user experience of websites. In HTML, images are embedded using the <img> tag.
Attributes of the <img> Tag
Attribute
Description
`src`
Specifies the image source (URL or file path).
`alt`
Provides alternative text for accessibility and SEO.
`width`
Defines the image width (in pixels or percentage).
`height`
Defines the image height (in pixels or percentage).
`title`
Displays a tooltip when hovered over.
`loading`
Defines how the image should be loaded (`lazy` or `eager`).
Example
Responsive Images
To make images responsive, use CSS or the `width` attribute
Lazy Loading Images
To improve page performance, use the `loading="lazy"` attribute
Image Formats
Format
Description
JPEG
Best for photographs, supports compression.
PNG
Supports transparency, better for logos.
GIF
Supports animation, limited colors.
SVG
Scalable vector format, best for icons and graphics.
WebP
Modern format with better compression and quality.
Adding Links to Images
Background Images Using CSS
Accessibility Considerations
Always use the `alt` attribute to describe the image.
Use descriptive filenames for images.
Avoid using images for text content.
Best Practices
✔️Use responsive images for better user experience.
✔️Optimize images to reduce file size without losing quality.
✔️Use modern formats like WebP for better compression.
✔️Avoid using unnecessary large images that slow down page load time.
❌Do not use images without the `alt` attribute.
Understanding how to properly use images in HTML enhances the design and usability of web pages. By following best practices, you can ensure optimized, accessible, and responsive images on your website.